package com.example.book.controller;

import com.example.common.result.Result;
import com.example.book.api.dto.BookDTO;
import com.example.book.service.BookService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图书控制器
 */
@RestController
@RequestMapping("/api/books")
@ApiOperation("图书控制器")
public class BookController {

    /**
     * 注入需要的bean类
     */
    @Autowired
    private BookService bookService;

    //**************CRUD操作****************

    /**
     * 创建图书
     */
    @PostMapping
    @ApiOperation("创建图书")
    public Result<Long> createBook(@RequestBody BookDTO bookDTO) {
        Long bookId = bookService.createBook(bookDTO);
        return Result.success(bookId);
    }

    /**
     * 更新图书
     */
    @PutMapping
    @ApiOperation("更新图书")
    public Result<Boolean> updateBook(@RequestBody BookDTO bookDTO) {
        boolean success = bookService.updateBook(bookDTO);
        return Result.success(success);
    }

    /**
     * 删除图书
     */
    @DeleteMapping("/{bookId}")
    @ApiOperation("删除图书")
    public Result<Boolean> deleteBook(@PathVariable Long bookId) {
        boolean success = bookService.deleteBook(bookId);
        return Result.success(success);
    }

    /**
     * 根据ID查询图书
     */
    @GetMapping("/{bookId}")
    @ApiOperation("根据ID查询图书")
    public Result<BookDTO> getBookById(@PathVariable Long bookId) {
        BookDTO bookDTO = bookService.getBookById(bookId);
        return Result.success(bookDTO);
    }

    /**
     * 查询所有图书
     */
    @GetMapping
    @ApiOperation("查询所有图书")
    public Result<List<BookDTO>> listBooks() {
        List<BookDTO> bookList = bookService.listBooks();
        return Result.success(bookList);
    }

}
